package com.sh.business.service.impl;

import com.sh.business.config.RabbitmqSender;
import com.sh.business.domain.User;
import com.sh.business.domain.UserInvest;
import com.sh.business.domain.dto.DelayMessageDto;
import com.sh.business.mapper.UserMapper;
import com.sh.common.core.enums.MessageTypeEnum;
import com.sh.common.core.enums.OrderTypeEnum;
import com.sh.common.core.enums.TransferStatusEnum;
import com.sh.common.core.utils.DateUtils;
import com.sh.common.core.utils.MapstructUtils;
import com.sh.common.core.utils.OrderSnUtil;
import com.sh.common.core.utils.StringUtils;
import com.sh.common.mybatis.core.page.TableDataInfo;
import com.sh.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sh.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sh.business.domain.bo.UserRepurchaseBo;
import com.sh.business.domain.vo.UserRepurchaseVo;
import com.sh.business.domain.UserRepurchase;
import com.sh.business.mapper.UserRepurchaseMapper;
import com.sh.business.service.IUserRepurchaseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户充值复购u记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-29
 */
@RequiredArgsConstructor
@Service
public class UserRepurchaseServiceImpl implements IUserRepurchaseService {

    private final UserRepurchaseMapper baseMapper;
    private final UserMapper userMapper;
    private final RabbitmqSender rabbitmqSender;

    /**
     * 查询用户充值复购u记录
     *
     * @param id 主键
     * @return 用户充值复购u记录
     */
    @Override
    public UserRepurchaseVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户充值复购u记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户充值复购u记录分页列表
     */
    @Override
    public TableDataInfo<UserRepurchaseVo> queryPageList(UserRepurchaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserRepurchase> lqw = buildQueryWrapper(bo);
        Page<UserRepurchaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户充值复购u记录列表
     *
     * @param bo 查询条件
     * @return 用户充值复购u记录列表
     */
    @Override
    public List<UserRepurchaseVo> queryList(UserRepurchaseBo bo) {
        LambdaQueryWrapper<UserRepurchase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserRepurchase> buildQueryWrapper(UserRepurchaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserRepurchase> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserRepurchase::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderSn()), UserRepurchase::getOrderSn, bo.getOrderSn());
        lqw.eq(StringUtils.isNotBlank(bo.getTxHash()), UserRepurchase::getTxHash, bo.getTxHash());
        lqw.eq(bo.getStatus() != null, UserRepurchase::getStatus, bo.getStatus());
        lqw.eq(bo.getTransferTime() != null, UserRepurchase::getTransferTime, bo.getTransferTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFromAddress()), UserRepurchase::getFromAddress, bo.getFromAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getToAddress()), UserRepurchase::getToAddress, bo.getToAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getNotifyData()), UserRepurchase::getNotifyData, bo.getNotifyData());
        return lqw;
    }

    /**
     * 新增用户充值复购u记录
     *
     * @param bo 用户充值复购u记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserRepurchaseBo bo) {
        // 正式时使用
        User user = userMapper.selectById(LoginHelper.getUserId());
        UserRepurchase add = new UserRepurchase();
        add.setUserId(user.getUserId());
        add.setOrderSn(OrderSnUtil.generateOrderSn(OrderTypeEnum.CZ));
        add.setFromAddress(user.getWalletAddress());
        add.setStatus(TransferStatusEnum.WAIT_CONFIRM.getCode());
        add.setTxHash(bo.getTxHash());
        add.setPurchaseAmount(bo.getAmount());
        add.setAmount(bo.getAmount());

        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }

        // 发送延迟查询消息
        DelayMessageDto delayMessageDto = new DelayMessageDto();
        delayMessageDto.setTxHash(bo.getTxHash());
        delayMessageDto.setType(MessageTypeEnum.REPURCHASE.getCode());
        delayMessageDto.setCreateTime(DateUtils.getNowDate());
        // 第一次30秒后查询
        rabbitmqSender.sendTtlMsg(delayMessageDto, 30 * 1000L);

        return flag;
    }

    /**
     * 修改用户充值复购u记录
     *
     * @param bo 用户充值复购u记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserRepurchaseBo bo) {
        UserRepurchase update = MapstructUtils.convert(bo, UserRepurchase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserRepurchase entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户充值复购u记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
